package com.hpq.user.service.handler;

import com.hpq.user.service.WxMsgService;
import com.hpq.user.service.adapter.TextBuilder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
@Component
public class SubscribeHandler extends AbstractHandler{
    @Autowired
    private WxMsgService wxMsgService;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map, WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {
        this.log.info("新关注用户 OPENID: " + wxMpXmlMessage.getFromUser());

        WxMpXmlOutMessage responseResult = null;
        try {
            responseResult = wxMsgService.scan(wxMpXmlMessage);
        } catch (Exception e) {
            this.log.error(e.getMessage(), e);
        }
        if(responseResult != null){
            return responseResult;
        }

            return TextBuilder.build(wxMpXmlMessage, "感谢关注");

    }
}
